/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.lib.users;

import ac.settings.dataModel;
import ac.settings.files;
import ac.settings.sesstionData;
import ac.settings.sqlQuerys;
import ac.ui.utils;

/**
 *The class that has the permission checking for the different button actions.
 * @author empcl_000
 */
public class permissions implements dataModel, sqlQuerys, files {

    public static String usertype = ac.settings.sesstionData.userType;
    sesstionData s = new sesstionData();

    // ac.lib.users
    public permissions() {

    }
    ac.lib.database.operations db = new ac.lib.database.operations();
/**
 * 
 * @return String represents the currently logged in user type.
 */
    public String getUsertype() {
        return usertype;
    }
    /**
     * Sets the current user type
     * @param usertype 
     */

    public void setUsertype(String usertype) {
        this.usertype = usertype;
    }
/**
 * Returns the premission assigned to the current user type
 * @return 
 */
    public String[] getRow() {

        return db.getRow(TABLE_permissions, "userType='" + usertype + "'");

    }
/**
 * Determines if the action can be done with the current user type.
 * @param action the action to check its availability
 * @param sendMessage boolean flag to show an error message
 * @return true if the action is doable and false otherwise
 */
    public Boolean isCan(String action, boolean sendMessage) {
        boolean r = false;
        String[] acrions
                = getRow()[2].split(";");

        r = ac.lib.data.arrays.search(acrions, action);
        if (!r && sendMessage) {
            utils.sendcanPermissionsErrorMssage(null);
        }
        return r;

    }
/**
 * Check if a form can be viewed to the user or not.
 * @param view name of the form
 * @param sendMessage boolean flag to show an error message
 * @return true if the action is doable and false otherwise
 */
    public Boolean isView(String view, boolean sendMessage) {
        boolean r = false;
        String[] views
                = getRow()[3].split(";");

        r = ac.lib.data.arrays.search(views, view);

        if (!r && sendMessage) {
            utils.sendviewPermissionsErrorMssage(null);
        }
        return r;

    }

}
